package com.gjy.nwpufindseats.Fragment;

import com.gjy.nwpufindseats.util.CleanData;
import com.gjy.nwpufindseats.util.JwNetSpider;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

import com.gjy.nwpufindseats.Activity_Guide;
import com.gjy.nwpufindseats.Activity_aboutMe;
import com.gjy.nwpufindseats.Activity_coderNotice;
import com.gjy.nwpufindseats.Activity_lost;
import com.gjy.nwpufindseats.Activity_readme;
import com.gjy.nwpufindseats.Activity_study;
import com.gjy.nwpufindseats.Activity_tips;
import com.gjy.nwpufindseats.MainActivity;
import com.gjy.nwpufindseats.R;

public class Fragment_more extends Fragment implements OnClickListener {
	private View view;
	
	public static SharedPreferences lostPreferences;
	public static Editor lostEditor;
	private TextView lost;
	private TextView tips;
	private TextView readme;
	private TextView study;
	private TextView cleanCache;
	private TextView welcome;
	private TextView coderNotice;
	private TextView aboutMe;
	Context maincContext;
	private SharedPreferences prefRoomInfo;
	private Editor editorRoomInfo;
	public static ProgressDialog lostDialog;
	 @Override
	    public View onCreateView(LayoutInflater inflater, ViewGroup container,
	    		Bundle savedInstanceState) {
		
	    	view = inflater.inflate(R.layout.fragment_more, container, false);
	    	maincContext = getActivity();
	    	lost = (TextView) view.findViewById(R.id.lost);
	    	lost.setOnClickListener(this); 
	    	tips = (TextView) view.findViewById(R.id.tips);
	    	tips.setOnClickListener(this); 
	    	readme = (TextView) view.findViewById(R.id.readme);
	    	readme.setOnClickListener(this); 
	    	study = (TextView) view.findViewById(R.id.study);
	    	study.setOnClickListener(this); 
	    	cleanCache = (TextView) view.findViewById(R.id.cleanCache);
	    	cleanCache.setOnClickListener(this);
	    	welcome = (TextView) view.findViewById(R.id.welcome);
	    	welcome.setOnClickListener(this);
	    	coderNotice = (TextView) view.findViewById(R.id.coderNotice);
	    	coderNotice.setOnClickListener(this); 
	    	aboutMe = (TextView) view.findViewById(R.id.aboutMe);
	    	aboutMe.setOnClickListener(this); 
	    	 lostPreferences = getActivity().getSharedPreferences("user", Context.MODE_PRIVATE);
	    	lostEditor= lostPreferences.edit();
	    	return view;
	    }
	     
		@Override
		public void onClick(View v) {
			Intent intent = null;
			switch (v.getId()) {
			case R.id.lost:
				if(lostPreferences.getBoolean("method", true))
				{
					//没有翱翔门户的登陆记录
					Toast.makeText(getActivity(), "此功能您需要以“翱翔工大”方式登录", Toast.LENGTH_SHORT).show();
					Fragment newContent = new Fragment_login_info();
					switchFragment(newContent);
				}
				else{
					lostDialog=ProgressDialog.show(getActivity(), "正在登录", "奋力为您登陆中。请稍候！",true);
					lostDialog.setCancelable(true);           
					lostDialog.show();
					JwNetSpider.resetHc();//转换了登录账户，以前的静态变量hc必须重置	
					intent = new Intent(getActivity(),Activity_lost.class);
					startActivity(intent);
				}
				
				break;
			case R.id.tips:
				intent = new Intent(getActivity(),Activity_tips.class);
				startActivity(intent);
				break;
			case R.id.readme:
				intent = new Intent(getActivity(),Activity_readme.class);
				startActivity(intent);
				break;
			case R.id.study:
				intent = new Intent(getActivity(),Activity_study.class);
			startActivity(intent);
			break;
			case R.id.cleanCache:
				dialog();
				break;
			case R.id.welcome:
				Bundle bundle = new Bundle();
				bundle.putBoolean("first", false);
				intent = new Intent(getActivity(),Activity_Guide.class);
				intent.putExtras(bundle);
				startActivity(intent);
				break;
			case R.id.coderNotice:
				intent = new Intent(getActivity(),Activity_coderNotice.class);
				startActivity(intent);
				break;
			case R.id.aboutMe:
				intent = new Intent(getActivity(),Activity_aboutMe.class);
				startActivity(intent);
				break;
			default:
			break;
			}			
		}
		


		public void dialog() {
			  AlertDialog.Builder builder = new Builder(getActivity());
			  
			  builder.setTitle("确认要清除缓存吗？");
			  builder.setMessage("新的一周的数据会覆盖掉以前的数据，并不会造成大量数据积累，我们并不建议您清除下载数据。");
			  //不同的监听器
			  builder.setPositiveButton("确认", new android.content.DialogInterface.OnClickListener() {
				   @Override
				   public void onClick(DialogInterface dialog, int which) {
					CleanData.cleanFiles(maincContext);
					prefRoomInfo = getActivity().getSharedPreferences("downloaddate", Context.MODE_PRIVATE);
					editorRoomInfo = prefRoomInfo.edit();
					editorRoomInfo.clear();
					editorRoomInfo.commit();
				    dialog.dismiss();
				    Toast.makeText(getActivity(), "清除成功！", Toast.LENGTH_SHORT).show();
				   }

			   });
			  builder.setNegativeButton("取消", new android.content.DialogInterface.OnClickListener() {
			   @Override
			   public void onClick(DialogInterface dialog, int which) {
			    dialog.dismiss();
			   }
			  });
			  builder.create().show();
			}
	    private void switchFragment(Fragment fragment) {
			if (getActivity() == null)
				return;	
				MainActivity ra = (MainActivity) getActivity();
				ra.switchContent(fragment);
			
		}
		public static ProgressDialog getLostDialog() {
			return lostDialog;
		}

		public static void setLostDialog(ProgressDialog lostDialog) {
			Fragment_more.lostDialog = lostDialog;
		}
}
